import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import { Alert, Form, Switch, Icon, Input, Select, Button,  message, Radio } from 'antd'
const FormItem = Form.Item;
const Option = Select.Option;
const TextArea = Input.TextArea;
const RadioGroup = Radio.Group;
import creditModelService from 'containers/credit-setting/credit-model/credit-model.service'
import errorMessage from 'share/errorMessage'

class NewCreditRulesGroup extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: {}
    };
  }

  onCancel = () => {
    this.props.close();
  };

  componentWillReceiveProps(nextProps){
    if(nextProps.params.showSlideFrame && !this.props.params.showSlideFrame){
      this.props.form.resetFields()
    }
  }

  handleSave = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        this.setState({ loading: true });
        values.modelId = this.props.creditModel.id;
        creditModelService.saveCreditRulesGroup(values).then(res => {
          this.setState({ loading: false });
          message.success(messages('credit-setting.key45')/*保存成功*/);
          this.props.close(true);
        }).catch(e => {
          errorMessage(e.response);
          this.setState({ loading: false });
        })
      }
    });
  };

  render(){
    const { getFieldDecorator } = this.props.form;
    const { loading } = this.state;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 10, offset: 1 },
    };
    return (
      <div>
        <Alert
          message={messages('common.help')/*提示信息*/}
          description={messages('credit-setting.key118')/*同一信用模型下允许设置多套信用管理规则集；仅能存在一套节点管理类型的规则集；仅能存在一套定时管理类型的规则集；规则集代码需租户下唯一。*/}
          type="info"
          showIcon
        />
        <Form onSubmit={this.handleSave} style={{ marginTop: 30 }}>
          <FormItem {...formItemLayout} label={messages('credit-setting.key51')/*规则集代码*/}>
            {getFieldDecorator('ruleSetCode', {
              rules: [
                {
                  required: true,
                  message: messages('common.please.enter')
                },
              ]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('credit-setting.key52')/*规则集名称*/}>
            {getFieldDecorator('ruleSetName', {
              rules: [
                {
                  required: true,
                  message: messages('common.please.enter')
                },
              ]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('credit-setting.key53')/*规则类型*/}>
            {getFieldDecorator('ruleTypeId',{
              initialValue: 1002,
            })(
              <RadioGroup>
                <Radio value={1001}>{messages('credit-setting.key112')/*节点管理*/}</Radio>
                <Radio value={1002}>{messages('credit-setting.key54')/*手动管理*/}</Radio>
                <Radio value={1003}>{messages('credit-setting.key113')/*定时管理*/}</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('common.remark')/*备注*/}>
            {getFieldDecorator('comment', {
              rules: [
                {
                  required: true,
                  message: messages('common.please.enter')
                },
              ]
            })(
              <TextArea autosize={{minRows: 2}}
                        maxLength="500"
                        style={{minWidth:'100%'}}
                        placeholder={messages('common.please.enter')/*请输入*/}/>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('common.column.status')/*状态*/}>
            {getFieldDecorator('valid',{
              initialValue: true,
              valuePropName: 'checked'
            })(
              <Switch/>
            )}
          </FormItem>
          <div className="slide-footer">
            <Button type="primary" htmlType="submit" loading={loading}>{messages('common.save')/* 保存 */}</Button>
            <Button onClick={this.onCancel}>{messages('common.cancel')/* 取消 */}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

function mapStateToProps(state) {
  return {
    creditModel: state.creditSetting.creditModel
  }
}
const WrappedNewCreditRulesGroup = Form.create()(NewCreditRulesGroup);

export default connect(mapStateToProps)(WrappedNewCreditRulesGroup);
